<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.10"/>
<title>Introduction_to_Algorithms: src/string_matching_algorithms/finite_automaton_match/finiteautomatonmatch.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Introduction_to_Algorithms
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.10 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('finiteautomatonmatch_8h_source.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">finiteautomatonmatch.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="finiteautomatonmatch_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016- huaxz &lt;huaxz1986@163.com&gt;</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * This program is free software: you can redistribute it and/or modify</span></div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * it under the terms of the GNU General Public License as published by</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * the Free Software Foundation, either version 2 of the License, or</span></div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> * (at your option) any later version.</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> * This program is distributed in the hope that it will be useful,</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * GNU General Public License for more details.</span></div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * You should have received a copy of the GNU General Public License</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> * along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="comment"> *</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;<span class="comment"> * Author: huaxz1986@163.com (huaxz)</span></div>
<div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="comment"> */</span></div>
<div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#ifndef AUTOMATONMATCH</span></div>
<div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;<span class="preprocessor">#define AUTOMATONMATCH</span></div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#include&lt;vector&gt;</span></div>
<div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;<span class="preprocessor">#include&lt;ostream&gt;</span></div>
<div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a> {</div>
<div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    <span class="keyword">namespace </span>StringMatchingAlgorithm{</div>
<div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;</div>
<div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;</div>
<div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> Iterator&gt; <span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::difference_type</div>
<div class="line"><a name="l00040"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#acf5d8c82dffae3894905d8091defb315">   40</a></span>&#160;                                                <a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#acf5d8c82dffae3894905d8091defb315">index_of_M</a>(Iterator beginM,Iterator endM,</div>
<div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;                                              <span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::value_type a)</div>
<div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;        {</div>
<div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;            <span class="keyword">auto</span> len_M=std::distance(beginM,endM);</div>
<div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;            <span class="keywordflow">if</span>(len_M&lt;0)</div>
<div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;index_of_M error: must be endM&gt;beginM!&quot;</span>);</div>
<div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;</div>
<div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;            <span class="keyword">auto</span> iter=beginM;</div>
<div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;            <span class="keyword">auto</span> result_iter=endM;</div>
<div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;            <span class="keywordflow">while</span>(iter&lt;endM)</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;            {</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;                <span class="keywordflow">if</span>(*iter==a)</div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;                {</div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;                    result_iter=iter;</div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;                    iter++;</div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;                    <span class="keywordflow">break</span>;</div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;                }</div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;                iter++;</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;            }</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;            std::ostringstream os;</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;            os&lt;&lt;a;</div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;            <span class="keywordflow">while</span>(iter&lt;endM) <span class="comment">//继续查找，有重复字符则抛出异常</span></div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;            {</div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;                <span class="keywordflow">if</span>(*iter==a) <span class="keywordflow">throw</span> std::invalid_argument(std::string(<span class="stringliteral">&quot;index_of_M error: M has dulplicate character &quot;</span>)+os.str()+<span class="stringliteral">&quot;!&quot;</span>);</div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;                iter++;</div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;            }</div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;            <span class="keywordflow">if</span>(result_iter==endM) <span class="keywordflow">throw</span> std::invalid_argument(std::string(<span class="stringliteral">&quot;index_of_M error: M has no charactor &quot;</span>)+os.str()+<span class="stringliteral">&quot;!&quot;</span>);</div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;            <span class="keywordflow">return</span> std::distance(beginM,result_iter);</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;        }</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;</div>
<div class="line"><a name="l00083"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a4267339182491a371210d73c3be57ba1">   83</a></span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> Iterator&gt; <span class="keywordtype">bool</span> <a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a4267339182491a371210d73c3be57ba1">is_end_with</a>(Iterator begin,Iterator k_iter,Iterator q_iter,</div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;                                              <span class="keyword">typename</span> std::iterator_traits&lt;Iterator&gt;::value_type a)</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;        {</div>
<div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;            <span class="keyword">auto</span> k=std::distance(begin,k_iter);</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;            <span class="keyword">auto</span> q=std::distance(begin,q_iter);</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;            <span class="keywordflow">if</span>(k&lt;0||q&lt;0)</div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;is_end_with error:must be k_iter&gt;=begin and q_iter&gt;=begin!&quot;</span>);</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;            <span class="keywordflow">if</span>(k==0) <span class="keywordflow">return</span> <span class="keyword">true</span>; <span class="comment">// 空字符串是所有字符串的后缀</span></div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;            <span class="keywordflow">if</span>(a!=*(k_iter-1)) <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">//P[k]!=a</span></div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;k-1;i++)</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;                <span class="keywordflow">if</span>(*(k_iter-2-i)!=*(q_iter-1-i))<span class="keywordflow">return</span> <span class="keyword">false</span>;  <span class="comment">//P[k-i-1]!=P[q-i]</span></div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;            <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;        }</div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;</div>
<div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;        <span class="keyword">template</span>&lt;<span class="keyword">typename</span> PIterator,<span class="keyword">typename</span> MIterator&gt;</div>
<div class="line"><a name="l00120"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a2096b4363b80c224193aee054d71cf97">  120</a></span>&#160;        <span class="keywordtype">void</span> <a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a2096b4363b80c224193aee054d71cf97">get_delta</a>(<span class="keyword">const</span> PIterator P_begin,<span class="keyword">const</span> PIterator P_end,</div>
<div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;                      <span class="keyword">const</span> MIterator M_begin,<span class="keyword">const</span> MIterator M_end,std::vector&lt;std::vector&lt;int&gt;&gt;&amp;delta)</div>
<div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;        {</div>
<div class="line"><a name="l00123"></a><span class="lineno">  123</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;PIterator&gt;::value_type T1;</div>
<div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;MIterator&gt;::value_type T2;</div>
<div class="line"><a name="l00125"></a><span class="lineno">  125</span>&#160;            static_assert(std::is_same&lt;T1, T2&gt;::value,<span class="stringliteral">&quot;get_delt error:mode sequence P and sequence M must contain same type value!&quot;</span>);</div>
<div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;</div>
<div class="line"><a name="l00127"></a><span class="lineno">  127</span>&#160;            <span class="keyword">auto</span> p_len=std::distance(P_begin,P_end);</div>
<div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;            <span class="keyword">auto</span> m_len=std::distance(M_begin,M_end);</div>
<div class="line"><a name="l00129"></a><span class="lineno">  129</span>&#160;            <span class="keywordflow">if</span>(p_len&lt;0)</div>
<div class="line"><a name="l00130"></a><span class="lineno">  130</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;finite_automaton_match error:P_begin must &lt;= P_end&quot;</span>);</div>
<div class="line"><a name="l00131"></a><span class="lineno">  131</span>&#160;            <span class="keywordflow">if</span>(m_len&lt;=0)</div>
<div class="line"><a name="l00132"></a><span class="lineno">  132</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;finite_automaton_match error:M_begin must &lt; M_end&quot;</span>);</div>
<div class="line"><a name="l00133"></a><span class="lineno">  133</span>&#160;</div>
<div class="line"><a name="l00134"></a><span class="lineno">  134</span>&#160;            delta.clear(); <span class="comment">//矩阵清零</span></div>
<div class="line"><a name="l00135"></a><span class="lineno">  135</span>&#160;</div>
<div class="line"><a name="l00136"></a><span class="lineno">  136</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> q=0;q&lt;=p_len;q++)</div>
<div class="line"><a name="l00137"></a><span class="lineno">  137</span>&#160;            {</div>
<div class="line"><a name="l00138"></a><span class="lineno">  138</span>&#160;                delta.push_back(std::vector&lt;int&gt;()); <span class="comment">//添加一行</span></div>
<div class="line"><a name="l00139"></a><span class="lineno">  139</span>&#160;</div>
<div class="line"><a name="l00140"></a><span class="lineno">  140</span>&#160;                <span class="keyword">auto</span> m_iter=M_begin;</div>
<div class="line"><a name="l00141"></a><span class="lineno">  141</span>&#160;                <span class="keywordflow">while</span>(m_iter&lt;M_end)</div>
<div class="line"><a name="l00142"></a><span class="lineno">  142</span>&#160;                {</div>
<div class="line"><a name="l00143"></a><span class="lineno">  143</span>&#160;                    <span class="comment">//*********** 寻找Pk是 (Pq a) 后缀的最大的k ***********</span></div>
<div class="line"><a name="l00144"></a><span class="lineno">  144</span>&#160;                    <span class="keywordtype">int</span> k=std::min(p_len+1,q+2);</div>
<div class="line"><a name="l00145"></a><span class="lineno">  145</span>&#160;                    <span class="keywordflow">do</span>{</div>
<div class="line"><a name="l00146"></a><span class="lineno">  146</span>&#160;                        k--;</div>
<div class="line"><a name="l00147"></a><span class="lineno">  147</span>&#160;                    }</div>
<div class="line"><a name="l00148"></a><span class="lineno">  148</span>&#160;                    <span class="keywordflow">while</span>(!<a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a4267339182491a371210d73c3be57ba1">is_end_with</a>(P_begin,P_begin+k,P_begin+q,*(m_iter)));</div>
<div class="line"><a name="l00149"></a><span class="lineno">  149</span>&#160;                    <span class="comment">//******  delt(q,a)=k *******</span></div>
<div class="line"><a name="l00150"></a><span class="lineno">  150</span>&#160;                    delta[delta.size()-1].push_back(k);<span class="comment">//最后一行中添加列数据</span></div>
<div class="line"><a name="l00151"></a><span class="lineno">  151</span>&#160;                    m_iter++;</div>
<div class="line"><a name="l00152"></a><span class="lineno">  152</span>&#160;                }</div>
<div class="line"><a name="l00153"></a><span class="lineno">  153</span>&#160;            }</div>
<div class="line"><a name="l00154"></a><span class="lineno">  154</span>&#160;</div>
<div class="line"><a name="l00155"></a><span class="lineno">  155</span>&#160;        }</div>
<div class="line"><a name="l00156"></a><span class="lineno">  156</span>&#160;</div>
<div class="line"><a name="l00158"></a><span class="lineno">  158</span>&#160;</div>
<div class="line"><a name="l00249"></a><span class="lineno">  249</span>&#160;       <span class="keyword">template</span>&lt;<span class="keyword">typename</span> IteratorT,<span class="keyword">typename</span> IteratorP,<span class="keyword">typename</span> IteratorM&gt;</div>
<div class="line"><a name="l00250"></a><span class="lineno"><a class="line" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#aa156c6aecec5459b601ae0069725330e">  250</a></span>&#160;       std::vector&lt;int&gt; <a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#aa156c6aecec5459b601ae0069725330e">finite_automaton_match</a>(<span class="keyword">const</span> IteratorT iterT_begin,<span class="keyword">const</span> IteratorT iterT_end,</div>
<div class="line"><a name="l00251"></a><span class="lineno">  251</span>&#160;             <span class="keyword">const</span> IteratorP iterP_begin,<span class="keyword">const</span> IteratorP iterP_end,</div>
<div class="line"><a name="l00252"></a><span class="lineno">  252</span>&#160;             <span class="keyword">const</span> IteratorM iterM_begin,<span class="keyword">const</span> IteratorM iterM_end)</div>
<div class="line"><a name="l00253"></a><span class="lineno">  253</span>&#160;       {</div>
<div class="line"><a name="l00254"></a><span class="lineno">  254</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;IteratorT&gt;::value_type T1;</div>
<div class="line"><a name="l00255"></a><span class="lineno">  255</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;IteratorP&gt;::value_type T2;</div>
<div class="line"><a name="l00256"></a><span class="lineno">  256</span>&#160;            <span class="keyword">typedef</span> <span class="keyword">typename</span> std::iterator_traits&lt;IteratorM&gt;::value_type T3;</div>
<div class="line"><a name="l00257"></a><span class="lineno">  257</span>&#160;</div>
<div class="line"><a name="l00258"></a><span class="lineno">  258</span>&#160;            static_assert(std::is_same&lt;T1, T2&gt;::value,<span class="stringliteral">&quot;finite_automaton_match error:sequence T and P must contain same type value!&quot;</span>);</div>
<div class="line"><a name="l00259"></a><span class="lineno">  259</span>&#160;            static_assert(std::is_same&lt;T1, T3&gt;::value,<span class="stringliteral">&quot;finite_automaton_match error:sequence T and M must contain same type value!&quot;</span>);</div>
<div class="line"><a name="l00260"></a><span class="lineno">  260</span>&#160;</div>
<div class="line"><a name="l00261"></a><span class="lineno">  261</span>&#160;            <span class="keyword">auto</span> lenT=std::distance(iterT_begin,iterT_end);</div>
<div class="line"><a name="l00262"></a><span class="lineno">  262</span>&#160;            <span class="keyword">auto</span> lenP=std::distance(iterP_begin,iterP_end);</div>
<div class="line"><a name="l00263"></a><span class="lineno">  263</span>&#160;            <span class="keyword">auto</span> lenM=std::distance(iterM_begin,iterM_end);</div>
<div class="line"><a name="l00264"></a><span class="lineno">  264</span>&#160;            <span class="keywordflow">if</span>(lenT&lt;0)</div>
<div class="line"><a name="l00265"></a><span class="lineno">  265</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;finite_automaton_match error:iterT_begin must &lt;= iterT_end&quot;</span>);</div>
<div class="line"><a name="l00266"></a><span class="lineno">  266</span>&#160;            <span class="keywordflow">if</span>(lenP&lt;=0)</div>
<div class="line"><a name="l00267"></a><span class="lineno">  267</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;finite_automaton_match error:iterP_begin must &lt; iterP_end&quot;</span>);</div>
<div class="line"><a name="l00268"></a><span class="lineno">  268</span>&#160;            <span class="keywordflow">if</span>(lenM&lt;=0)</div>
<div class="line"><a name="l00269"></a><span class="lineno">  269</span>&#160;                <span class="keywordflow">throw</span> std::invalid_argument(<span class="stringliteral">&quot;finite_automaton_match error:iterM_begin must &lt; iterM_end&quot;</span>);</div>
<div class="line"><a name="l00270"></a><span class="lineno">  270</span>&#160;</div>
<div class="line"><a name="l00271"></a><span class="lineno">  271</span>&#160;            std::vector&lt;int&gt; result;</div>
<div class="line"><a name="l00272"></a><span class="lineno">  272</span>&#160;            <span class="keywordflow">if</span>(lenT&lt;lenP) <span class="comment">//模式串P较长，直接返回</span></div>
<div class="line"><a name="l00273"></a><span class="lineno">  273</span>&#160;                <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00274"></a><span class="lineno">  274</span>&#160;            std::vector&lt;std::vector&lt;int&gt;&gt; delta;</div>
<div class="line"><a name="l00275"></a><span class="lineno">  275</span>&#160;</div>
<div class="line"><a name="l00276"></a><span class="lineno">  276</span>&#160;            <span class="comment">//**********  预处理  **************</span></div>
<div class="line"><a name="l00277"></a><span class="lineno">  277</span>&#160;            <a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a2096b4363b80c224193aee054d71cf97">get_delta</a>(iterP_begin,iterP_end,iterM_begin,iterM_end,delta);</div>
<div class="line"><a name="l00278"></a><span class="lineno">  278</span>&#160;</div>
<div class="line"><a name="l00279"></a><span class="lineno">  279</span>&#160;            <span class="comment">//*********** 匹配 ***************</span></div>
<div class="line"><a name="l00280"></a><span class="lineno">  280</span>&#160;            <span class="keywordtype">int</span> q=0;</div>
<div class="line"><a name="l00281"></a><span class="lineno">  281</span>&#160;            <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i=0;i&lt;lenT;i++)</div>
<div class="line"><a name="l00282"></a><span class="lineno">  282</span>&#160;            {</div>
<div class="line"><a name="l00283"></a><span class="lineno">  283</span>&#160;                <span class="keyword">auto</span>  T_i_index=<a class="code" href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#acf5d8c82dffae3894905d8091defb315">index_of_M</a>(iterM_begin,iterM_end,*(iterT_begin+i));</div>
<div class="line"><a name="l00284"></a><span class="lineno">  284</span>&#160;                q=delta[q][T_i_index];</div>
<div class="line"><a name="l00285"></a><span class="lineno">  285</span>&#160;                <span class="keywordflow">if</span>(q==lenP)</div>
<div class="line"><a name="l00286"></a><span class="lineno">  286</span>&#160;                {</div>
<div class="line"><a name="l00287"></a><span class="lineno">  287</span>&#160;                    result.push_back(i-lenP+1); <span class="comment">//[0,1,...i] ，其右侧长度为lenP的区间为[i-lenP+1,i-lenP+2,...i]</span></div>
<div class="line"><a name="l00288"></a><span class="lineno">  288</span>&#160;                }</div>
<div class="line"><a name="l00289"></a><span class="lineno">  289</span>&#160;            }</div>
<div class="line"><a name="l00290"></a><span class="lineno">  290</span>&#160;            <span class="keywordflow">return</span> result;</div>
<div class="line"><a name="l00291"></a><span class="lineno">  291</span>&#160;       }</div>
<div class="line"><a name="l00292"></a><span class="lineno">  292</span>&#160;    }</div>
<div class="line"><a name="l00293"></a><span class="lineno">  293</span>&#160;}</div>
<div class="line"><a name="l00294"></a><span class="lineno">  294</span>&#160;<span class="preprocessor">#endif // AUTOMATONMATCH</span></div>
<div class="line"><a name="l00295"></a><span class="lineno">  295</span>&#160;</div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_string_matching_algorithm_html_acf5d8c82dffae3894905d8091defb315"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#acf5d8c82dffae3894905d8091defb315">IntroductionToAlgorithm::StringMatchingAlgorithm::index_of_M</a></div><div class="ttdeci">std::iterator_traits&lt; Iterator &gt;::difference_type index_of_M(Iterator beginM, Iterator endM, typename std::iterator_traits&lt; Iterator &gt;::value_type a)</div><div class="ttdoc">index_of_M ： 返回返回字符a，算法导论第32章32.3节 </div><div class="ttdef"><b>Definition:</b> <a href="finiteautomatonmatch_8h_source.html#l00040">finiteautomatonmatch.h:40</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_string_matching_algorithm_html_aa156c6aecec5459b601ae0069725330e"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#aa156c6aecec5459b601ae0069725330e">IntroductionToAlgorithm::StringMatchingAlgorithm::finite_automaton_match</a></div><div class="ttdeci">std::vector&lt; int &gt; finite_automaton_match(const IteratorT iterT_begin, const IteratorT iterT_end, const IteratorP iterP_begin, const IteratorP iterP_end, const IteratorM iterM_begin, const IteratorM iterM_end)</div><div class="ttdoc">finite_automaton_match： 有限自动机字符串匹配算法，算法导论第32章32.3节 </div><div class="ttdef"><b>Definition:</b> <a href="finiteautomatonmatch_8h_source.html#l00250">finiteautomatonmatch.h:250</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_string_matching_algorithm_html_a2096b4363b80c224193aee054d71cf97"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a2096b4363b80c224193aee054d71cf97">IntroductionToAlgorithm::StringMatchingAlgorithm::get_delta</a></div><div class="ttdeci">void get_delta(const PIterator P_begin, const PIterator P_end, const MIterator M_begin, const MIterator M_end, std::vector&lt; std::vector&lt; int &gt;&gt; &amp;delta)</div><div class="ttdoc">get_delt： 获取模式字符串的转移函数，算法导论第32章32.3节 </div><div class="ttdef"><b>Definition:</b> <a href="finiteautomatonmatch_8h_source.html#l00120">finiteautomatonmatch.h:120</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_1_1_string_matching_algorithm_html_a4267339182491a371210d73c3be57ba1"><div class="ttname"><a href="namespace_introduction_to_algorithm_1_1_string_matching_algorithm.html#a4267339182491a371210d73c3be57ba1">IntroductionToAlgorithm::StringMatchingAlgorithm::is_end_with</a></div><div class="ttdeci">bool is_end_with(Iterator begin, Iterator k_iter, Iterator q_iter, typename std::iterator_traits&lt; Iterator &gt;::value_type a)</div><div class="ttdoc">is_end_with： 返回Pk是否是( Pq a)的后缀，算法导论第32章32.3节 </div><div class="ttdef"><b>Definition:</b> <a href="finiteautomatonmatch_8h_source.html#l00083">finiteautomatonmatch.h:83</a></div></div>
<div class="ttc" id="namespace_introduction_to_algorithm_html"><div class="ttname"><a href="namespace_introduction_to_algorithm.html">IntroductionToAlgorithm</a></div><div class="ttdoc">Namespace of IntrodunctionToAlgorithm. </div><div class="ttdef"><b>Definition:</b> <a href="longest__common__subsequence_8h_source.html#l00024">longest_common_subsequence.h:24</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_8ed9dba4cd616b67da9b1338594e34e0.html">src</a></li><li class="navelem"><a class="el" href="dir_f4a7ab032f7e09156dc21182a68c14c9.html">string_matching_algorithms</a></li><li class="navelem"><a class="el" href="dir_a1e550cf5358937b404b93753542c521.html">finite_automaton_match</a></li><li class="navelem"><a class="el" href="finiteautomatonmatch_8h.html">finiteautomatonmatch.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.10 </li>
  </ul>
</div>
</body>
</html>
